using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO;
using System.Security.Permissions;
using System.Web;
using System.Xml;
using PickGold.Charting.Utilities;

namespace PickGold.Charting
{
	[DefaultProperty("Format"), SRDescription("DescriptionAttributeChartSerializer_ChartSerializer"), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class ChartSerializer
	{
		private Chart _chart;
		private SerializationContents _content;
		private SerializationFormat _format;
		private SerializerBase _serializer;
		private IServiceContainer _serviceContainer;

		private ChartSerializer()
		{
			this._serializer = new XmlFormatSerializer();
			this._content = SerializationContents.Default;
		}

		internal ChartSerializer(IServiceContainer container)
		{
			this._serializer = new XmlFormatSerializer();
			this._content = SerializationContents.Default;
			if (container == null)
			{
				throw new ArgumentNullException(SR.ExceptionInvalidServiceContainer);
			}
			this._serviceContainer = container;
		}

		internal Chart GetChartObject()
		{
			if (this._chart == null)
			{
				this._chart = (Chart)this._serviceContainer.GetService(typeof(Chart));
			}
			return this._chart;
		}

		protected string GetContentString(SerializationContents content, bool serializable)
		{
			switch (content)
			{
				case SerializationContents.Default:
					return "";

				case SerializationContents.Data:
					if (!serializable)
					{
						return "";
					}
					return "Chart.BuildNumber, Chart.Series, Series.Points, Series.Name, DataPoint.XValue, DataPoint.YValues,DataPoint.LabelStyle,DataPoint.AxisLabel,DataPoint.LabelFormat,DataPoint.IsEmpty, Series.YValuesPerPoint, Series.IsXValueIndexed, Series.XValueType, Series.YValueType";

				case SerializationContents.Appearance:
					if (!serializable)
					{
						return "";
					}
					return "Chart.BuildNumber, *.Name*, *.Fore*, *.Back*, *.Border*, *.Line*, *.Frame*, *.PageColor*, *.SkinStyle*, *.Palette, *.PaletteCustomColors, *.Font*, *.*Font, *.Color, *.Shadow*, *.MarkerColor, *.MarkerStyle, *.MarkerSize, *.MarkerBorderColor, *.MarkerImage, *.MarkerImageTransparentColor, *.LabelBackColor, *.LabelBorder*, *.Enable3D, *.IsRightAngleAxes, *.IsClustered, *.LightStyle, *.Perspective, *.Inclination, *.Rotation, *.PointDepth, *.PointGapDepth, *.WallWidth";

				case SerializationContents.All:
					return "";
			}
			throw new InvalidOperationException(SR.ExceptionChartSerializerContentFlagUnsupported);
		}

		internal object GetService(Type serviceType)
		{
			if (serviceType != typeof(ChartSerializer))
			{
				throw new ArgumentException(SR.ExceptionChartSerializerUnsupportedType(serviceType.ToString()));
			}
			return this;
		}

		public void Load(Stream stream)
		{
			if (stream == null)
			{
				throw new ArgumentNullException("stream");
			}
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Loading;
			}
			this._serializer.Deserialize(this.GetChartObject(), stream);
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		public void Load(TextReader reader)
		{
			if (reader == null)
			{
				throw new ArgumentNullException("reader");
			}
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Loading;
			}
			this._serializer.Deserialize(this.GetChartObject(), reader);
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		public void Load(string fileName)
		{
			if (fileName == null)
			{
				throw new ArgumentNullException("fileName");
			}
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Loading;
			}
			this._serializer.Deserialize(this.GetChartObject(), fileName);
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		public void Load(XmlReader reader)
		{
			if (reader == null)
			{
				throw new ArgumentNullException("reader");
			}
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Loading;
			}
			this._serializer.Deserialize(this.GetChartObject(), reader);
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		public void Reset()
		{
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Resetting;
			}
			this._serializer.ResetObjectProperties(this.GetChartObject());
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		public void Save(Stream stream)
		{
			if (stream == null)
			{
				throw new ArgumentNullException("stream");
			}
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Saving;
			}
			this.GetChartObject().ResetAutoValues();
			this._serializer.Serialize(this.GetChartObject(), stream);
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		public void Save(TextWriter writer)
		{
			if (writer == null)
			{
				throw new ArgumentNullException("writer");
			}
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Saving;
			}
			this.GetChartObject().ResetAutoValues();
			this._serializer.Serialize(this.GetChartObject(), writer);
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		public void Save(string fileName)
		{
			if (fileName == null)
			{
				throw new ArgumentNullException("fileName");
			}
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Saving;
			}
			this.GetChartObject().ResetAutoValues();
			this._serializer.Serialize(this.GetChartObject(), fileName);
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		public void Save(XmlWriter writer)
		{
			if (writer == null)
			{
				throw new ArgumentNullException("writer");
			}
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = true;
				this.GetChartObject().SerializationStatus = SerializationStatus.Saving;
			}
			this.GetChartObject().ResetAutoValues();
			this._serializer.Serialize(this.GetChartObject(), writer);
			if (this.GetChartObject() != null)
			{
				this.GetChartObject().Serializing = false;
				this.GetChartObject().SerializationStatus = SerializationStatus.None;
			}
		}

		internal void SetSerializableContent()
		{
			this.SerializableContent = "";
			this.NonSerializableContent = "";
			foreach (object obj2 in Enum.GetValues(typeof(SerializationContents)))
			{
				if (obj2 is SerializationContents)
				{
					SerializationContents content = (SerializationContents)obj2;
					if ((((this.Content & content) == content) && (content != SerializationContents.All)) && (this.Content != SerializationContents.All))
					{
						if (this.NonSerializableContent.Length != 0)
						{
							this.NonSerializableContent = this.NonSerializableContent + ", ";
						}
						this.NonSerializableContent = this.NonSerializableContent + this.GetContentString(content, false);
						this.NonSerializableContent = this.NonSerializableContent.TrimStart(new char[] { ',' });
						if (this.SerializableContent.Length != 0)
						{
							this.SerializableContent = this.SerializableContent + ", ";
						}
						this.SerializableContent = this.SerializableContent + this.GetContentString(content, true);
						this.SerializableContent = this.SerializableContent.TrimStart(new char[] { ',' });
					}
				}
			}
		}

		[SRCategory("CategoryAttributeMisc"), DefaultValue(typeof(SerializationContents), "Default"), SRDescription("DescriptionAttributeChartSerializer_Content")]
		public SerializationContents Content
		{
			get
			{
				return this._content;
			}
			set
			{
				this._content = value;
				this.SetSerializableContent();
			}
		}

		[SRDescription("DescriptionAttributeChartSerializer_Format"), DefaultValue(typeof(SerializationFormat), "Xml"), SRCategory("CategoryAttributeMisc")]
		public SerializationFormat Format
		{
			get
			{
				return this._format;
			}
			set
			{
				if (this._format != value)
				{
					this._format = value;
					SerializerBase base2 = null;
					if (this._format == SerializationFormat.Binary)
					{
						base2 = new BinaryFormatSerializer();
					}
					else
					{
						base2 = new XmlFormatSerializer();
					}
					base2.IsUnknownAttributeIgnored = this._serializer.IsUnknownAttributeIgnored;
					base2.NonSerializableContent = this._serializer.NonSerializableContent;
					base2.IsResetWhenLoading = this._serializer.IsResetWhenLoading;
					base2.SerializableContent = this._serializer.SerializableContent;
					this._serializer = base2;
				}
			}
		}

		[DefaultValue(true), SRDescription("DescriptionAttributeChartSerializer_ResetWhenLoading"), SRCategory("CategoryAttributeMisc")]
		public bool IsResetWhenLoading
		{
			get
			{
				return this._serializer.IsResetWhenLoading;
			}
			set
			{
				this._serializer.IsResetWhenLoading = value;
			}
		}

		[SRCategory("CategoryAttributeMisc"), DefaultValue(false), SRDescription("DescriptionAttributeChartSerializer_TemplateMode")]
		public bool IsTemplateMode
		{
			get
			{
				return this._serializer.IsTemplateMode;
			}
			set
			{
				this._serializer.IsTemplateMode = value;
			}
		}

		[SRDescription("DescriptionAttributeChartSerializer_IgnoreUnknownXmlAttributes"), SRCategory("CategoryAttributeMisc"), DefaultValue(false)]
		public bool IsUnknownAttributeIgnored
		{
			get
			{
				return this._serializer.IsUnknownAttributeIgnored;
			}
			set
			{
				this._serializer.IsUnknownAttributeIgnored = value;
			}
		}

		[SRCategory("CategoryAttributeMisc"), DefaultValue(""), SRDescription("DescriptionAttributeChartSerializer_NonSerializableContent")]
		public string NonSerializableContent
		{
			get
			{
				return this._serializer.NonSerializableContent;
			}
			set
			{
				this._serializer.NonSerializableContent = value;
			}
		}

		[SRCategory("CategoryAttributeMisc"), SRDescription("DescriptionAttributeChartSerializer_SerializableContent"), DefaultValue("")]
		public string SerializableContent
		{
			get
			{
				return this._serializer.SerializableContent;
			}
			set
			{
				this._serializer.SerializableContent = value;
			}
		}
	}
}

